﻿using System;
using System.Collections.ObjectModel;
using System.Linq;

namespace PDFDesign.Model
{
    public class TreeNodeModel
    {
        public TreeNodeType TreeNodeType { get; set; }
        public string Name { get; set; }
        public object Tag { get; set; }
        public string Color { get; set; } = "Black";
        public TreeNodeModel Parent { get; set; }
        public ObservableCollection<TreeNodeModel> Children { get; set; }

        public static ObservableCollection<TreeNodeModel> Convert(NewStyle newStyle)
        {
            var pageSettingNode = new TreeNodeModel
            {
                TreeNodeType = TreeNodeType.PageSetting,
                Name = "PageSetting",
                Color = "Red",
                Tag = newStyle?.PageSetting
            };
            var controlsNode = new TreeNodeModel
            {
                TreeNodeType = TreeNodeType.Controls,
                Name = "Controls",
                Color = "Red",
                Tag = newStyle?.Controls
            };
            controlsNode.Children = new ObservableCollection<TreeNodeModel>(newStyle?.Controls.Select(control => new TreeNodeModel
            {
                TreeNodeType = TreeNodeType.Control,
                Name = $"Control[{control.Name}]",
                Tag = control,
                Parent = controlsNode
            }));
            var tableNode = new TreeNodeModel
            {
                TreeNodeType = TreeNodeType.Table,
                Name = "Table",
                Color = "Red",
                Tag = newStyle.Table,
                Children = new ObservableCollection<TreeNodeModel>()
            };
            var columnsNode = new TreeNodeModel
            {
                TreeNodeType = TreeNodeType.Columns,
                Name = "Columns",
                Color = "Red",
                Tag = newStyle?.Table?.Columns,
                Parent = tableNode
            };
            columnsNode.Children = new ObservableCollection<TreeNodeModel>(newStyle?.Table?.Columns.Select(column => new TreeNodeModel
            {
                TreeNodeType = TreeNodeType.Column,
                Name = $"Column[{column.Name}]",
                Tag = column,
                Parent = columnsNode
            }));
            var rowsNode = new TreeNodeModel
            {
                TreeNodeType = TreeNodeType.Rows,
                Name = "Rows",
                Color = "Red",
                Tag = newStyle?.Table?.Rows,
                Parent = tableNode
            };
            rowsNode.Children = new ObservableCollection<TreeNodeModel>(newStyle?.Table?.Rows.Select(row =>
            {
                var rowNode = new TreeNodeModel
                {
                    TreeNodeType = TreeNodeType.Row,
                    Name = "Row",
                    Tag = row,
                    Parent = rowsNode
                };
                rowNode.Children = new ObservableCollection<TreeNodeModel>(row.Cells.Select(cell => new TreeNodeModel
                {
                    TreeNodeType = TreeNodeType.Cell,
                    Name = $"Cell[{cell.Name}]",
                    Tag = cell,
                    Parent = rowNode
                }));
                return rowNode;
            }));
            var fixedRowsNode = new TreeNodeModel
            {
                TreeNodeType = TreeNodeType.FixedRows,
                Name = "FixedRows",
                Color = "Red",
                Tag = newStyle?.Table?.FixedRows,
                Parent = tableNode
            };
            fixedRowsNode.Children = new ObservableCollection<TreeNodeModel>(newStyle?.Table?.FixedRows.Select(row =>
            {
                var rowNode = new TreeNodeModel
                {
                    TreeNodeType = TreeNodeType.Row,
                    Name = "Row",
                    Tag = row,
                    Parent = fixedRowsNode
                };
                rowNode.Children = new ObservableCollection<TreeNodeModel>(row.Cells.Select(cell => new TreeNodeModel
                {
                    TreeNodeType = TreeNodeType.Cell,
                    Name = $"Cell[{cell.Name}]",
                    Tag = cell,
                    Parent = rowNode
                }));
                return rowNode;
            }));
            tableNode.Children.Add(columnsNode);
            tableNode.Children.Add(rowsNode);
            tableNode.Children.Add(fixedRowsNode);
            return new ObservableCollection<TreeNodeModel>
            {
                pageSettingNode,
                controlsNode,
                tableNode
            }; ;
        }
    }

    public enum TreeNodeType
    {
        PageSetting,
        Controls,
        Control,
        Table,
        Columns,
        Column,
        FixedRows,
        Rows,
        Row,
        Cell,
    }
}
